package cn.iocoder.yudao.module.trade.service.giftcard;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.trade.controller.admin.giftcard.vo.CardGroupPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.giftcard.vo.CardGroupSaveReqVO;
import cn.iocoder.yudao.module.trade.controller.app.giftcard.vo.AppGiftCardSpuReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftCardGroupDO;
import cn.iocoder.yudao.module.trade.dal.mysql.giftcard.GiftCardGroupMapper;
import cn.iocoder.yudao.module.trade.enums.giftcard.GiftCardGroupTypeEnum;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.GiftCardGroupDO_NOT;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.GiftWxCardDO;

@Service
public class GiftCardGroupServiceImpl implements GiftCardGroupService {

    @Resource
    private GiftCardGroupMapper giftCardGroupMapper;

    @Override
    public GiftCardGroupDO selectById(Long id) {
        return giftCardGroupMapper.selectById(id);
    }

    @Override
    public List<GiftCardGroupDO> selectGiftCardGroupList(String cardId, Integer groupType) {
        return giftCardGroupMapper.selectListByCardId(cardId, groupType);
    }


    @Override
    public Long createCardGroup(CardGroupSaveReqVO createReqVO) {
        List<AppGiftCardSpuReqVO> giftCardSpuReqList = createReqVO.getGiftCardSpuReqList();
        if (null != giftCardSpuReqList && !giftCardSpuReqList.isEmpty()) {
            createReqVO.setGiftCardSpuReq(JSON.toJSONString(giftCardSpuReqList));
        }
        // 插入
        GiftCardGroupDO cardGroup = BeanUtils.toBean(createReqVO, GiftCardGroupDO.class);
        giftCardGroupMapper.insert(cardGroup);
        // 返回
        return cardGroup.getId();
    }

    @Override
    public void updateCardGroup(CardGroupSaveReqVO updateReqVO) {
        List<AppGiftCardSpuReqVO> giftCardSpuReqList = updateReqVO.getGiftCardSpuReqList();
        if (null != giftCardSpuReqList && !giftCardSpuReqList.isEmpty()) {
            updateReqVO.setGiftCardSpuReq(JSON.toJSONString(giftCardSpuReqList));
        }
        // 校验存在
        validateCardGroupExists(updateReqVO.getId());
        // 更新
        GiftCardGroupDO updateObj = BeanUtils.toBean(updateReqVO, GiftCardGroupDO.class);
        giftCardGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteCardGroup(Long id) {
        // 校验存在
        validateCardGroupExists(id);
        // 删除
        giftCardGroupMapper.deleteById(id);
    }

    private void validateCardGroupExists(Long id) {
        if (giftCardGroupMapper.selectById(id) == null) {
            throw exception(GiftCardGroupDO_NOT);
        }
    }

    @Override
    public GiftCardGroupDO getCardGroup(Long id) {
        return giftCardGroupMapper.selectById(id);
    }

    @Override
    public PageResult<GiftCardGroupDO> getCardGroupPage(CardGroupPageReqVO pageReqVO) {
        return giftCardGroupMapper.selectPage(pageReqVO);
    }


}
